package cn.kgc.ebook.controller;

import cn.kgc.ebook.pojo.Ebookentry;
import cn.kgc.ebook.pojo.ResultEntity;
import cn.kgc.ebook.service.EbookKentryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class KentryController {

    @Resource
    EbookKentryService ebookKentryService;

    @RequestMapping("/seleKentry")
    public ResultEntity<PageInfo<Ebookentry>> seleKentry(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize,
                                                         @RequestParam(value = "categoryId",required = false,defaultValue = "0")String categoryId){
        PageInfo<Ebookentry> select = ebookKentryService.select(pageNum, pageSize, categoryId);
    return ResultEntity.successWithoutData(select);
    }


//    根据id删除数据
    @RequestMapping("/deleKentry")
    public ResultEntity<String> deleKentry(Integer id){
        try {

            Integer delete = ebookKentryService.delete(id);
            return ResultEntity.successWithoutData(delete+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }
    //添加图书信息
    @RequestMapping("/addKentry")
    public ResultEntity<String> addKentry(@RequestBody Ebookentry ebookentry){

        Integer update;
        try {

            if(ebookentry.getId()==null){
                //添加
                update =ebookKentryService.add(ebookentry);
            }else{
                //修改
                update = ebookKentryService.update(ebookentry);
            }
            return ResultEntity.successWithoutData(update+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }

    //根据id查询图书信息
    @RequestMapping("/seleById")
    public ResultEntity<Ebookentry> seleById(Integer id){
        Ebookentry ebookentry = ebookKentryService.seleById(id);
    return ResultEntity.successWithoutData(ebookentry);
    }

}
